package com.glory.wxmemsicpda.adapter

import android.opengl.Visibility
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.base.App
import com.glory.wxmemsicpda.base.Constant
import com.glory.wxmemsicpda.base.DataBindBaseViewHolder
import com.glory.wxmemsicpda.bean.MaterialsListBean
import com.glory.wxmemsicpda.bean.SheetInfoBean
import com.glory.wxmemsicpda.databinding.AdapterChangeMaterialsListBinding
import com.glory.wxmemsicpda.databinding.AdapterUpdateMaterialQtyBinding

class UpdateMaterialQTYAdapter(data: List<MaterialsListBean.MaterialsBean?>?) :
    BaseQuickAdapter<MaterialsListBean.MaterialsBean?, DataBindBaseViewHolder>(
        R.layout.adapter_update_material_qty,
        data as MutableList<MaterialsListBean.MaterialsBean?>?
    ) {
    private var mCallBack: CallBack? = null

    override fun convert(helper: DataBindBaseViewHolder, item: MaterialsListBean.MaterialsBean?) {
        val binding: AdapterUpdateMaterialQtyBinding =
            helper.getDataBinding() as AdapterUpdateMaterialQtyBinding
        binding.productBean = item
        binding.executePendingBindings()
        setEditListener(binding.etQty, item)

        binding.ivArrow.setOnClickListener{
            if (binding.llLot.visibility == View.GONE){
                binding.llLot.visibility = View.VISIBLE
                binding.llTime.visibility = View.VISIBLE
                binding.llType.visibility = View.VISIBLE
               binding.ivArrow.setImageResource(R.drawable.free_ui_glory_down_arrow)
            }else{
                binding.llLot.visibility = View.GONE
                binding.llTime.visibility = View.GONE
                binding.llType.visibility = View.GONE
                binding.ivArrow.setImageResource(R.drawable.free_ui_glory_more)

            }
        }


    }

    fun setCallBack(callBack: CallBack) {
        this.mCallBack = callBack
    }

    interface CallBack {
        fun show(materialsBean: MaterialsListBean.MaterialsBean)


    }

    private fun setEditListener(
        editText: EditText,
        item: MaterialsListBean.MaterialsBean?
    ) {
        if (editText.tag is TextWatcher) {
            editText.removeTextChangedListener(editText.tag as TextWatcher);
        }

        var textWatcher: TextWatcher = object : TextWatcher {

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable) {
                item?.qty = s.toString()
            }
        }

        editText.addTextChangedListener(textWatcher);
        editText.tag = textWatcher;
    }

}